﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SystemModule;

namespace M2Server
{
    public partial class TPlayObject
    {

        /// <summary>
        /// 创建英雄
        /// </summary>
        /// <returns></returns>
        public unsafe TBaseObject MakeHero(TPlayObject PlayObject, THumDataInfo HumanRcd)
        {
            TBaseObject result = null;
            int nX = 0;
            int nY = 0;
            int n1C = 0;
            int n10 = 0;
            TBaseObject MonObj;
            byte nCode = 0;
            try
            {
                this.GetFrontPosition(ref nX, ref nY);
                nCode = 1;
                if ((((PlayObject.m_sHeroCharName).ToLower().CompareTo((HumanRcd.Data.sChrName).ToLower()) != 0)
                    && ((PlayObject.m_sDeputyHeroCharName).ToLower().CompareTo((HumanRcd.Data.sChrName).ToLower()) != 0)) ||
                    (!HumanRcd.Header.boIsHero) || (HumanRcd.Header.boDeleted))
                {
                    return result;
                }
                nCode = 2;
                while (true)
                {
                    if (!this.m_PEnvir.CanWalk(nX, nY, false))// 如果坐标无法走，则取主人的方向，计算可以走到的坐标上
                    {
                        this.GetFrontPosition(ref nX, ref nY);
                        n10 = (this.m_btDirection + n1C) % 8;
                        MakeHero_GetGotoXY(n10, ref nX, ref nY);
                    }
                    else
                    {
                        break;
                    }
                    n1C++;
                    if (n1C >= 9)
                    {
                        break;
                    }
                }
                MonObj = M2Share.UserEngine.RegenMyHero(PlayObject, nX, nY, HumanRcd);
                if (MonObj != null)
                {
                    nCode = 3;
                    MonObj.m_Master = this;
                    MonObj.m_dwMasterRoyaltyTick = 1000000; // 1000 * 1000
                    MonObj.m_dwMasterRoyaltyTime = HUtil32.GetTickCount();
                    MonObj.m_btSlaveMakeLevel = (byte)this.m_Abil.Level;
                    MonObj.m_btSlaveExpLevel = (byte)this.m_Abil.MaxExp;
                    nCode = 4;
                    MonObj.RecalcAbilitys();
                    nCode = 5;
                    MonObj.CompareSuitItem(false);// 套装
                    if (MonObj.m_WAbil.HP <= 0)
                    {
                        MonObj.m_WAbil.HP = (MonObj.m_WAbil.MaxHP / 15) + 2;
                    }
                    nCode = 6;
                    MonObj.RefNameColor();// 刷新名字颜色
                    nCode = 7;
                    if (MonObj.m_Master != null) // 修正面对墙壁，障碍物，召唤英雄时，英雄会离主人很远，如超过4格，则飞到主人身旁
                    {
                        nCode = 8;
                        if ((Math.Abs(MonObj.m_nCurrX - MonObj.m_Master.m_nCurrX) > 4) || (Math.Abs(MonObj.m_nCurrY - MonObj.m_Master.m_nCurrY) > 4))
                        {
                            nCode = 9;
                            MonObj.SpaceMove(MonObj.m_Master.m_PEnvir.sMapName, MonObj.m_Master.m_nCurrX, MonObj.m_Master.m_nCurrY, 1);
                        }
                    }
                    result = MonObj;
                }
            }
            catch
            {
                M2Share.MainOutMessage("[异常] TPlayObject.MakeHero Code:" + nCode);
            }
            return result;
        }

        // --------双英雄相关--------//
        private void HeroAutoTraining()
        {
        }

        // 客户端发送确定评定副将英雄
        private void ClientAssessHero()
        {
            if (AssessHeroDataInfo[0].IsMain)
            {
                sName1 = AssessHeroDataInfo[0].HeroDataInfo.sChrName;
                sName2 = AssessHeroDataInfo[1].HeroDataInfo.sChrName;
            }
            else if (AssessHeroDataInfo[1].IsMain)
            {
                sName2 = AssessHeroDataInfo[0].HeroDataInfo.sChrName;
                sName1 = AssessHeroDataInfo[1].HeroDataInfo.sChrName;
            }
            M2Share.FrontEngine.AddToLoadHeroRcdList("", sName1 + "/" + sName2, this, 5);
        }

        // 确定评定主副将英雄
        private void ClientQueryAssessHero()
        {
            THeroDataInfo cu;
            string s1;
            string s2;
            //ZeroMemory(cu, sizeof(THeroDataInfo));
            if (m_sAssessHeroMsg != "")
            {
                s1 = m_sAssessHeroMsg;
                for (int i = 0; i <= 1; i++)
                {
                    //s1 = HUtil32.GetValidStr3(s1, ref s2, new string[] { "/" });
                    //DecodeBuffer(s2, cu, sizeof(THeroDataInfo));
                    //AssessHeroDataInfo[i].HeroDataInfo = cu;
                }
            }
            if ((AssessHeroDataInfo[0].HeroDataInfo.sChrName != "") && (AssessHeroDataInfo[1].HeroDataInfo.sChrName != ""))
            {
                if (AssessHeroDataInfo[0].HeroDataInfo.Level > AssessHeroDataInfo[1].HeroDataInfo.Level)
                {
                    AssessHeroDataInfo[0].IsMain = true;
                    AssessHeroDataInfo[1].IsMain = false;
                }
                else
                {
                    AssessHeroDataInfo[0].IsMain = false;
                    AssessHeroDataInfo[1].IsMain = true;
                }
            }
            //this.SendMsg(this, Grobal2.RM_QUERYASSESSHERO, 0, 0, 0, 0, EncryptUnit.EncodeBuffer(AssessHeroDataInfo[0], sizeof(TAssessHeroDataInfo)) + "/" + EncryptUnit.EncodeBuffer(AssessHeroDataInfo[1], sizeof(TAssessHeroDataInfo)));
        }

        // -------连击相关-------//
        private void ClientQueryOpenPulse(ushort PulsePage, ushort OpenPulse, bool IsHero)
        {
            if (M2Share.g_BatterNPC != null)
            {
                if (IsHero)
                {
                    if (m_MyHero != null)
                    {
                        M2Share.g_BatterNPC.GotoLable(this, "@HeroPulse" + (PulsePage).ToString() + "-" + (OpenPulse).ToString(), false);
                    }
                }
                else
                {
                    M2Share.g_BatterNPC.GotoLable(this, "@Pulse" + (PulsePage).ToString() + "-" + (OpenPulse).ToString(), false);
                }
            }
        }

        public void ClientUseBatter_GetRandomOrder()
        {
            ArrayList List = null;
            TUserMagic UserMagic;
            int K;
            try
            {
                for (int i = 0; i <= 2; i++)
                {
                    this.bOrder[i] = this.m_nBatterOrder[i];
                }
                List = new ArrayList();
                for (int i = 0; i < this.m_MagicList.Count; i++)
                {
                    UserMagic = this.m_MagicList[i];
                    if ((UserMagic != null) && (UserMagic.MagicInfo.sDescr == "连击")
                        && (UserMagic.MagicInfo.btJob == this.m_btJob))
                    {
                        List.Add(UserMagic.wMagIdx);
                    }
                }
                for (int i = List.Count - 1; i >= 0; i--)
                {
                    for (int j = 0; j <= 2; j++)
                    {
                        if (((int)List[i]) == this.bOrder[j])
                        {
                            List.RemoveAt(i);
                            break;
                        }
                    }
                }
                for (int i = 0; i <= 2; i++)
                {
                    if (this.bOrder[i] == 2222)
                    {
                        if (List.Count > 0)
                        {
                            K = HUtil32.Random(List.Count - 1);
                            if (K >= 0)
                            {
                                this.bOrder[i] = ((byte)List[K]);
                                List.RemoveAt(K);
                            }
                        }
                    }
                }
            }
            finally
            {
                Dispose(List);
            }
        }

        public unsafe void ClientUseBatter_ExitBatter()
        {
            m_boCanWalk = true;
            m_boCanRun = true;
            m_boCanHit = true;
            m_boCanSpell = true;
            this.m_boUseBatter = false;
            this.SendMsg(this, Grobal2.RM_BATTERUSEFINALLY, 0, 0, 0, 0, "");
        }

        /// <summary>
        /// 使用连击
        /// </summary>
        /// <param name="ProcessMessage"></param>
        public unsafe void ClientUseBatter(TProcessMessage ProcessMessage)
        {
            uint dwTime = 0;
            if (this.m_nBatter == 0)
            {
                ClientUseBatter_GetRandomOrder();
                this.m_nBatterSpellTick = HUtil32.GetTickCount() - 1500;
                this.m_boBatterFinally = true;
            }
            if (this.m_boBatterFinally && ((this.m_btJob == 0) || (HUtil32.GetTickCount() - this.m_nBatterSpellTick > 1200)))
            {
                if (this.m_nBatter > 2)
                {
                    ClientUseBatter_ExitBatter();
                    return;
                }
                if (this.bOrder[this.m_nBatter] == 0)
                {
                    ClientUseBatter_ExitBatter();
                    return;
                }
                this.m_nBatterSpellTick = HUtil32.GetTickCount();
                switch (this.m_btJob)
                {
                    case 0:
                    case 3:
                        this.m_boBatterFinally = false;
                        this.m_nBatterUseTick = HUtil32.GetTickCount();
                        ClientSpellXY(this.bOrder[this.m_nBatter], 0, 0, 0, null, true, ref dwTime);
                        break;
                    case 1:
                    case 2:
                        this.m_boBatterFinally = false;
                        m_boCanWalk = false;
                        m_boCanRun = false;
                        m_boCanHit = false;
                        m_boCanSpell = false;
                        if (this.m_nBatterTarget != null)
                        {
                            ClientSpellXY(this.bOrder[this.m_nBatter], 0, this.m_nBatterX, this.m_nBatterY, this.m_nBatterTarget, true, ref dwTime);
                        }
                        else
                        {
                            ClientSpellXY(this.bOrder[this.m_nBatter], 0, this.m_nBatterX, this.m_nBatterY, null, true, ref dwTime);
                        }
                        break;
                }
                this.m_nBatter++;
            }
        }
    }
}
